package com.nondo.dean.auth.service.impl;

import com.nondo.dean.auth.domain.Office;
import com.nondo.dean.auth.domain.OfficeExample;
import com.nondo.dean.auth.mapper.OfficeMapper;
import com.nondo.dean.auth.service.OfficeService;
import com.nondo.dean.common.web.Page;
import com.nondo.dean.common.web.Query;
import org.apache.commons.lang.StringUtils;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * project：dean-cloud
 *
 * @author linfeng @ nondo
 * @date 2018/4/23
 */
@Service
public class OfficeServiceImpl implements OfficeService {

    @Autowired
    private OfficeMapper officeMapper;

    @Override
    public Office findByOfficeId(Long officeId) {
        return officeMapper.selectByPrimaryKey(officeId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteByOfficeId(Long officeId) {
        officeMapper.deleteByPrimaryKey(officeId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveAndUpdateOffice(Office record) {
        if (record.getId() == null) {
            officeMapper.insertSelective(record);
        } else {
            officeMapper.updateByPrimaryKeySelective(record);
        }
    }

    @Override
    public void updateCompanyNameByCompanyId(String companyName, Long companyId) {

        Office office = new Office();
        office.setCompanyName(companyName);
        OfficeExample example = new OfficeExample();
        example.createCriteria().andCompanyIdEqualTo(companyId);

        officeMapper.updateByExample(office, example);
    }

    @Override
    public List<Office> findAll() {
        OfficeExample example = new OfficeExample();
        example.setOrderByClause("name asc");
        return officeMapper.selectByExample(example);
    }

    @Override
    public List<Office> findOfficeByCompanyId(Long companyId) {
        OfficeExample example = new OfficeExample();
        if (companyId != null) {
            example.createCriteria().andCompanyIdEqualTo(companyId);
        }
        example.setOrderByClause("name asc");
        return officeMapper.selectByExample(example);
    }

    @Override
    public Page<Office> findOfficePage(Query<Office> query) {
        OfficeExample example = new OfficeExample();
        OfficeExample.Criteria criteria = example.createCriteria();
        Office item = query.getData();
        if (item != null) {
            if (item.getId() != null) {
                criteria.andIdEqualTo(item.getId());
            }
            if (item.getCompanyId() != null) {
                criteria.andCompanyIdEqualTo(item.getCompanyId());
            }
            if (item.getParentId() != null) {
                criteria.andParentIdEqualTo(item.getParentId());
            }
            if (StringUtils.isNotBlank(item.getName())) {
                criteria.andNameLike("%" + item.getName() + "%");
            }
        }

        example.setOrderByClause("id asc");

        List<Office> resultList = officeMapper.selectByExampleWithRowbounds(example, new RowBounds(query.getOffset(), query.getLimit()));
        long total = officeMapper.countByExample(example);

        Page<Office> resultPage = Page.of(query);
        resultPage.setTotalElements(total);
        resultPage.setContent(resultList);

        return resultPage;
    }

}
